package com.qingke.easyjava.besuper2.command.impi;

import com.qingke.easyjava.besuper2.BeSuperApplication;
import com.qingke.easyjava.besuper2.PlayerDao;
import com.qingke.easyjava.besuper2.command.SystemCommand;
import com.qingke.easyjava.besuper2.pojo.Player;
import com.qingke.easyjava.utils.QingkeConsole;

public class LoginCommand extends SystemCommand {

	@Override
	public void execute() {

		String username = QingkeConsole.askUserInputString("Please enter your username");
		String password = QingkeConsole.askUserInputString("Please enter your password");

		PlayerDao pd = new PlayerDao();
		if (!pd.isUsernameExists(username)) {
			QingkeConsole.println("Username error");
			return;
		}
		Player player = pd.login(username, password);
		if (player == null) {
			QingkeConsole.println("Password error");
			return;
		}
		
		String name = (player.getNickname().length() > 0) ? player.getNickname() : player.getUsername();
		QingkeConsole.println("Welcome to back," + name);
		BeSuperApplication.getInstance().setPlayerProfile(player);
		

	}

}
